const Model = require('./index')

class UtilsModel extends Model {
  async findConfig () {
    const sql = 'select * from config order by id asc'
    try {
      const data = await this.db.query(sql)
      return data[0]
    } catch (error) {
      throw new Error(error)
    }
  }

  async updateConfig (attrs) {
    const { siteName, recordNo, copyright, version, siteLogo, smtpHost, smtpPort, smtpUsername, smtpPassword, smtpSendEmail, baiduAK } = attrs
    const { columnSet, values } = this.multipleColumnSet({
      siteName, recordNo, copyright, version, smtpHost, siteLogo, smtpPort, smtpUsername, smtpPassword, smtpSendEmail, baiduAK
    })
    const sql = `update config set ${columnSet}`
    try {
      await this.db.query(sql, values)
    } catch (error) {
      throw new Error(error)
    }
  }
}

module.exports = new UtilsModel()
